package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.service.WithdrawDepositService;
import com.yanzhu.service.model.WithdrawDeposit;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: 方炎柱
 * @Description: 提现模块
 * @Date: Created in 22:23 2018/1/21
 * @Modified:
 */
@AllArgsConstructor
@RestController
public class WithdrawDepositAPI {

    private WithdrawDepositService withdrawDepositService;

    //提现
    //TODO 需要修改 涉及到平台转账
    @RequestMapping(value="/userWithdrawDeposit")
    public ResponseBean insertRecharge(HttpServletRequest request){
        String uid = request.getParameter("uid");
        String withdraw_fee = request.getParameter("withdraw_fee");

        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");

        if(withdraw_fee == null || withdraw_fee.equals(""))
            return ResponseBean.fail("缺少提取金额参数！");

        WithdrawDeposit withdrawDeposit = new WithdrawDeposit();
        withdrawDeposit.setUid(Integer.parseInt(uid));
        withdrawDeposit.setWithdraw_fee(Float.parseFloat(withdraw_fee));
        withdrawDeposit.setWithdraw_time((int)(new Date().getTime()/1000));
        int result = withdrawDepositService.insert(withdrawDeposit);
        if(result < 1)
            return ResponseBean.fail("数据库错误！");
        return ResponseBean.ok(result);
    }

    @RequestMapping(value="/queryUserWithdrawDepositList")
    public ResponseBean queryWithdrawDepositList(HttpServletRequest request){
        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");

        String page = request.getParameter("page");
        if(page == null || page.equals(""))
            return ResponseBean.fail("缺少页数参数!");

        String size = request.getParameter("size");
        if(size == null || size.equals(""))
            return ResponseBean.fail("缺少页数大小参数!");

        return ResponseBean.ok(withdrawDepositService.queryWithdrawDepositList(Long.parseLong(uid), Integer.parseInt(page), Integer.parseInt(size)));
    }

    @RequestMapping(value="/deleteUserWithdrawDepositById")
    public ResponseBean deleteById(HttpServletRequest request){
        String id = request.getParameter("id");

        if(id == null || id.equals(""))
            return ResponseBean.fail("缺少ID参数！");

        if(withdrawDepositService.deleteWithdrawDepositById(Long.parseLong(id)) < 0)
            return ResponseBean.fail("数据库错误！");

        return ResponseBean.ok("删除成功！");
    }

    @RequestMapping(value="/deleteAllWithdrawDeposit")
    public ResponseBean deleteAll(HttpServletRequest request){
        String uid = request.getParameter("uid");

        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数！");
        if(withdrawDepositService.deleteWithdrawDepositAll(Long.parseLong(uid)) < 0)
            return ResponseBean.fail("数据库错误！");

        return ResponseBean.ok("删除成功！");
    }
}
